package com.lh1010.s3.builder;

import cn.hutool.core.util.StrUtil;
import com.zielsmart.beetlsql.starter.codegen.BaseProject;
import com.zielsmart.beetlsql.starter.codegen.Entity;
import com.zielsmart.beetlsql.starter.codegen.SourceConfig;
import com.zielsmart.beetlsql.starter.codegen.builder.BaseTemplateSourceBuilder;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.GroupTemplate;
import org.beetl.core.ResourceLoader;
import org.beetl.core.Template;

import java.io.Writer;
import java.util.Arrays;

@Slf4j
public class ServiceBuilder extends BaseTemplateSourceBuilder {

    public ServiceBuilder() {
        super("service");
    }

    @Override
    public void generate(BaseProject project, SourceConfig config, Entity entity) {
        Template template = this.getGroupTemplate("template").getTemplate("service.btl");
        String className = entity.getName();

        template.binding("className", className);
        template.binding("minClassName", StrUtil.lowerFirst(className));
        template.binding("package", project.getBasePackage(this.name));
        template.binding("entityClass", entity.getName());
        template.binding("comment", entity.getComment());
        template.binding("url", entity.getTableName().split("_")[1]);
        //得到生成的entity的包
        String entityPkg = project.getBasePackage("entity");
        String mapperHead =  entityPkg + ".*" ;
        template.binding("imports", Arrays.asList(mapperHead));
        Writer writer = project.getWriterByName(this.name,entity.getName()+"Service.java");
        template.renderTo(writer);
    }
}
